/**
 * Copyright &copy; com.smooth.common
 */
package com.smooth.site.sx.mag.basedata.college.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.smooth.common.site.common.config.Global;
import com.smooth.common.site.common.excep.SiteException;
import com.smooth.common.site.common.persistence.Page;
import com.smooth.common.site.common.utils.StringUtils;
import com.smooth.common.site.common.web.BaseController;
import com.smooth.dao.gen.BaseCollegeMapper;
import com.smooth.dao.gen.bean.BaseCollege;
import com.smooth.site.sx.mag.basedata.college.service.BaseCollegeService;

/**
 * 高校Controller
 * @author 北冥
 * @version 2018-12-31
 */
@Controller
@RequestMapping(value = "${adminPath}/college/baseCollege")
public class BaseCollegeController extends BaseController {

	@Autowired
	private BaseCollegeService baseCollegeService;
	
	@ModelAttribute
	public BaseCollege get(@RequestParam(required=false) String id,@RequestParam(required=false) String opt,HttpServletRequest request) {
		BaseCollege entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = baseCollegeService.get(id);
		}
		if (StringUtils.isNotBlank(opt)){
			request.setAttribute("opt", opt);
		}
		if (entity == null){
			entity = new BaseCollege();
		}
		return entity;
	}
	
	@RequiresPermissions("college:baseCollege:view")
	@RequestMapping(value = {"list", ""})
	public String list(BaseCollege baseCollege, HttpServletRequest request, HttpServletResponse response, Model model) {
		try{
			Page<BaseCollege> page = baseCollegeService.findPage(new Page<BaseCollege>(request, response), baseCollege); 
			model.addAttribute("page", page);
		}catch(SiteException e){
			model.addAttribute("message", e.getMessage());
			return "basedata/college/baseCollegeList";
		}
		return "basedata/college/baseCollegeList";
	}
	
	@RequiresPermissions("college:baseCollege:view")
	@RequestMapping(value = "form")
	public String form(BaseCollege baseCollege, Model model) {
		model.addAttribute("baseCollege", baseCollege);
		return "basedata/college/baseCollegeForm";
	}

	@RequiresPermissions("college:baseCollege:edit")
	@RequestMapping(value = "save")
	public String save(BaseCollege baseCollege, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, baseCollege)){
			return form(baseCollege, model);
		}
		try{
			baseCollegeService.save(baseCollege);
		}catch(SiteException e){
			addMessage(redirectAttributes, e.getMessage());
			return "redirect:"+Global.getAdminPath()+"/college/baseCollege?repage";
		}
		addMessage(redirectAttributes, "保存高校成功");
		return "redirect:"+Global.getAdminPath()+"/college/baseCollege?repage";
	}
	
	@RequiresPermissions("college:baseCollege:edit")
	@RequestMapping(value = "delete")
	public String delete(BaseCollege baseCollege, RedirectAttributes redirectAttributes) {
		try{
			baseCollegeService.delete(baseCollege);
		}catch(SiteException e){
			addMessage(redirectAttributes, e.getMessage());
			return "redirect:"+Global.getAdminPath()+"/college/baseCollege?repage";
		}
		addMessage(redirectAttributes, "删除高校成功");
		return "redirect:"+Global.getAdminPath()+"/college/baseCollege?repage";
	}

}